import { ref } from 'vue';

const loading = ref(false);
const showLoading = () => {
  if (!loading.value) {
    loading.value = true;
  }
};

const hideLoading = () => {
  if (loading.value) {
    loading.value = false;
  }
};

// 新建一个 MPromise 类型，用于在请求前展示 loading，请求返回后隐藏 loading
const MPromise = function () {};
MPromise.all = (requestArray) => {
  showLoading();
  return Promise.all(requestArray)
    .then((resuttArray) => {
      hideLoading();
      return resuttArray;
    })
    .catch((err) => {
      hideLoading();
      return err;
    });
};

export { loading, showLoading, hideLoading, MPromise };
